<!DOCTYPE html>
<html lang="zh">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
  <title>修改账户密码</title>
  <link rel="icon" href="favicon.ico" type="image/ico">
  <meta name="keywords" content="LightYear,光年,后台模板,后台管理系统,光年HTML模板">
  <meta name="description" content="LightYear是一个基于Bootstrap v3.3.7的后台管理系统的HTML模板。">
  <meta name="author" content="yinqi">
  <link href="../statics/css/bootstrap.min.css" rel="stylesheet">
  <link href="../statics/css/style.min.css" rel="stylesheet">
  <link href="../statics/css/materialdesignicons.min.css" rel="stylesheet">
  <link rel="stylesheet" type="text/css" href="../statics/css/verify.css">
  <link rel="stylesheet" href="../statics/css/iconfont.css" />
  <script src="../statics/js2/jquery-3.2.1.min.js"></script>
  <script src="../statics/css/semantic.js"></script>
  <script src="../statics/js2/verify.js"></script>
  <script src="../statics/js2/crypto-js.js"></script>
  <script src="../statics/js2/ase.js"></script>

  <link  href="../statics/css/screen.css"
         media="screen, projection"
         rel="stylesheet"
         type="text/css" />
</head>

<body>
<div class="lyear-layout-web">
  <div class="lyear-layout-container">
    <!--左侧导航-->
    <aside class="lyear-layout-sidebar">

      <!-- logo -->
      <div id="logo" class="sidebar-header">
        <a href="../book/index.html"><img src="../statics/images/logo-sidebar.png" title="LightYear" alt="LightYear"/></a>
      </div>
      <div class="lyear-layout-sidebar-scroll">

        <nav class="sidebar-main">
          <ul class="nav nav-drawer">
            <li class="nav-item active"><a href="../book/index.html"><i class="mdi mdi-home"></i> 首页</a></li>
            <li class="nav-item nav-item-has-subnav">
              <a href="javascript:void(0)"><i class="mdi mdi-palette"></i> 图书管理</a>
              <ul class="nav nav-subnav">
                <li><a href="../book/info.html">图书列表</a></li>
                <li><a href="../book/borrow.html">借阅记录</a></li>
                <li><a href="../book/type.html">图书类型</a></li>
              </ul>
            </li>
          </ul>
        </nav>
      </div>

    </aside>
    <!--End 左侧导航-->

    <!--头部信息-->
    <header class="lyear-layout-header">

      <nav class="navbar navbar-default">
        <div class="topbar">

          <div class="topbar-left">
            <div class="lyear-aside-toggler">
              <span class="lyear-toggler-bar"></span>
              <span class="lyear-toggler-bar"></span>
              <span class="lyear-toggler-bar"></span>
            </div>
            <span class="navbar-page-title"> 个人信息修改 </span>
          </div>

          <ul class="topbar-right">
            <li class="dropdown dropdown-profile">
              <a href="javascript:void(0)" data-toggle="dropdown">
                <img class="img-avatar img-avatar-48 m-r-10" src="../statics/images/users/avatar.jpg" alt="笔下光年" />
                <span>笔下光年 <span class="caret"></span></span>
              </a>
              <ul class="dropdown-menu dropdown-menu-right">
                <li> <a href="lyear_pages_profile.html"><i class="mdi mdi-account"></i> 个人信息</a> </li>
                <li> <a href="lyear_pages_edit_pwd.html"><i class="mdi mdi-lock-outline"></i> 修改密码</a> </li>
                <li> <a href="javascript:void(0)"><i class="mdi mdi-delete"></i> 清空缓存</a></li>
                <li class="divider"></li>
                <li> <a href="../index.html"><i class="mdi mdi-logout-variant"></i> 退出登录</a> </li>
              </ul>
            </li>
            <!--切换主题配色-->
            <li class="dropdown dropdown-skin">
              <span data-toggle="dropdown" class="icon-palette"><i class="mdi mdi-palette"></i></span>
              <ul class="dropdown-menu dropdown-menu-right" data-stopPropagation="true">
                <li class="drop-title"><p>主题</p></li>
                <li class="drop-skin-li clearfix">
                  <span class="inverse">
                    <input type="radio" name="site_theme" value="default" id="site_theme_1" checked>
                    <label for="site_theme_1"></label>
                  </span>
                  <span>
                    <input type="radio" name="site_theme" value="dark" id="site_theme_2">
                    <label for="site_theme_2"></label>
                  </span>
                  <span>
                    <input type="radio" name="site_theme" value="translucent" id="site_theme_3">
                    <label for="site_theme_3"></label>
                  </span>
                </li>
                <li class="drop-title"><p>LOGO</p></li>
                <li class="drop-skin-li clearfix">
                  <span class="inverse">
                    <input type="radio" name="logo_bg" value="default" id="logo_bg_1" checked>
                    <label for="logo_bg_1"></label>
                  </span>
                  <span>
                    <input type="radio" name="logo_bg" value="color_2" id="logo_bg_2">
                    <label for="logo_bg_2"></label>
                  </span>
                  <span>
                    <input type="radio" name="logo_bg" value="color_3" id="logo_bg_3">
                    <label for="logo_bg_3"></label>
                  </span>
                  <span>
                    <input type="radio" name="logo_bg" value="color_4" id="logo_bg_4">
                    <label for="logo_bg_4"></label>
                  </span>
                  <span>
                    <input type="radio" name="logo_bg" value="color_5" id="logo_bg_5">
                    <label for="logo_bg_5"></label>
                  </span>
                  <span>
                    <input type="radio" name="logo_bg" value="color_6" id="logo_bg_6">
                    <label for="logo_bg_6"></label>
                  </span>
                  <span>
                    <input type="radio" name="logo_bg" value="color_7" id="logo_bg_7">
                    <label for="logo_bg_7"></label>
                  </span>
                  <span>
                    <input type="radio" name="logo_bg" value="color_8" id="logo_bg_8">
                    <label for="logo_bg_8"></label>
                  </span>
                </li>
                <li class="drop-title"><p>头部</p></li>
                <li class="drop-skin-li clearfix">
                  <span class="inverse">
                    <input type="radio" name="header_bg" value="default" id="header_bg_1" checked>
                    <label for="header_bg_1"></label>
                  </span>
                  <span>
                    <input type="radio" name="header_bg" value="color_2" id="header_bg_2">
                    <label for="header_bg_2"></label>
                  </span>
                  <span>
                    <input type="radio" name="header_bg" value="color_3" id="header_bg_3">
                    <label for="header_bg_3"></label>
                  </span>
                  <span>
                    <input type="radio" name="header_bg" value="color_4" id="header_bg_4">
                    <label for="header_bg_4"></label>
                  </span>
                  <span>
                    <input type="radio" name="header_bg" value="color_5" id="header_bg_5">
                    <label for="header_bg_5"></label>
                  </span>
                  <span>
                    <input type="radio" name="header_bg" value="color_6" id="header_bg_6">
                    <label for="header_bg_6"></label>
                  </span>
                  <span>
                    <input type="radio" name="header_bg" value="color_7" id="header_bg_7">
                    <label for="header_bg_7"></label>
                  </span>
                  <span>
                    <input type="radio" name="header_bg" value="color_8" id="header_bg_8">
                    <label for="header_bg_8"></label>
                  </span>
                </li>
                <li class="drop-title"><p>侧边栏</p></li>
                <li class="drop-skin-li clearfix">
                  <span class="inverse">
                    <input type="radio" name="sidebar_bg" value="default" id="sidebar_bg_1" checked>
                    <label for="sidebar_bg_1"></label>
                  </span>
                  <span>
                    <input type="radio" name="sidebar_bg" value="color_2" id="sidebar_bg_2">
                    <label for="sidebar_bg_2"></label>
                  </span>
                  <span>
                    <input type="radio" name="sidebar_bg" value="color_3" id="sidebar_bg_3">
                    <label for="sidebar_bg_3"></label>
                  </span>
                  <span>
                    <input type="radio" name="sidebar_bg" value="color_4" id="sidebar_bg_4">
                    <label for="sidebar_bg_4"></label>
                  </span>
                  <span>
                    <input type="radio" name="sidebar_bg" value="color_5" id="sidebar_bg_5">
                    <label for="sidebar_bg_5"></label>
                  </span>
                  <span>
                    <input type="radio" name="sidebar_bg" value="color_6" id="sidebar_bg_6">
                    <label for="sidebar_bg_6"></label>
                  </span>
                  <span>
                    <input type="radio" name="sidebar_bg" value="color_7" id="sidebar_bg_7">
                    <label for="sidebar_bg_7"></label>
                  </span>
                  <span>
                    <input type="radio" name="sidebar_bg" value="color_8" id="sidebar_bg_8">
                    <label for="sidebar_bg_8"></label>
                  </span>
                </li>
              </ul>
            </li>
            <!--切换主题配色-->
          </ul>

        </div>
      </nav>

    </header>
    <!--End 头部信息-->

    <!--页面主要内容-->
    <main class="lyear-layout-content">

      <div class="container-fluid">

        <div class="row">
          <div class="col-lg-12">
            <div class="card">
              <div class="card-body">

                <form method="post" action="#!" class="site-form">
                  <div class="form-group">
                    <label for="old-password">旧密码</label>
                    <input type="password" class="form-control" name="oldpwd" id="old_pass" placeholder="输入账号的原登录密码">
                    <span id="s_old" class="span_error" ></span>
                  </div>
                  <div class="form-group">
                    <label for="new-password">新密码</label>
                    <input type="password" class="form-control" name="newpwd" id="new_password" placeholder="输入新的密码"
                           onkeyup="showPasswordError1()" onblur="showPasswordError1()">
                    <span id="s_new1" class="span_error" ></span>
                  </div>
                  <div class="form-group">
                    <label for="confirm-password">确认新密码</label>
                    <input type="password" class="form-control" name="confirmpwd" id="confirm_password" placeholder="请输入新的密码"
                           onkeyup="showPasswordError2()" onblur="showPasswordError2()">
                    <span id="s_new2" class="span_error" ></span>
                  </div>
                  <button id="update_btn" type="button" class="btn btn-primary">修改密码</button>
                </form>

              </div>
            </div>
          </div>

        </div>

      </div>

    </main>
    <!--End 页面主要内容-->
  </div>
  <!--错误提示-->
  <div id="ErrorMessage" class="tck hide"></div>
</div>

<script type="text/javascript" src="../statics/js/bootstrap.min.js"></script>
<script type="text/javascript" src="../statics/js/perfect-scrollbar.min.js"></script>
<script type="text/javascript" src="../statics/js/main.min.js"></script>
<script type="text/javascript" src="../statics/js2/md5.js"></script>
</body>

<script>
  //var URL = 'http://localhost:8088';
  var URL = 'http://39.107.205.22:8088';
  var user_id = window.sessionStorage.getItem("id")
  var user_name = window.sessionStorage.getItem("name")
  $(document).ready(function(){
    if(user_id == null){
      window.location.href  = '../index.html'
    }else{
      $.ajax({
        url:URL+"/user/selectUser",
        type:"GET",
        data:{"id":user_id,"name":user_name},
        dataType:"json",
        success:function(result){
          var userId = localStorage.getItem("id");
          console.log(userId)
          $("input[name='username']").val(result.data.name);
          $("input[name='nickname']").val(result.data.nick);
          $("input[name='email']").val(result.data.email);
        }
      })}
  });
  $("#update_btn").click(function (){
    if(user_id == null){
      window.location.href  = '../index.html'
    }else{
      $.ajax({
        url:URL+"/user/selectUser",
        type:"GET",
        data:{"name":"root"},
        dataType:"json",
        success:function(result){

          var password = result.data.password;
          var old_pass  = document.getElementById("old_pass").value;
          var new_password  = document.getElementById("new_password").value;
          var hash = hex_md5(old_pass)
          var hash_new = hex_md5(new_password)
          console.log(hash_new)

          var s_old  = document.getElementById("s_old");
          //旧密码和数据库中密码一致
          if(password == hash ){
            s_old.setAttribute("style","display:none");
            $.ajax({
              url:URL+"/user/updateUser",
              type:"POST",
              data:{"id":user_id,"name":user_name,"password":hash_new},
              dataType:"json",
              success:function(result){
                var userId = localStorage.getItem("id");
                console.log(userId)
                window.location.href ="/index.html"
              }
            })

          }else {
            s_old.setAttribute("style","display:block");
            s_old.innerHTML = "<p class='font_error'>原始密码不正确</p>";
          }


        }
      })}
  })



</script>

<script>


  /**
   * 密码校验
   */
  function showPasswordError1()  {
    //4.提示信息
    var s_new1  = document.getElementById("s_new1");
    var new_password = document.getElementById("new_password").value
    if (!new_password) {
      s_new1.setAttribute("style","display:block");
      s_new1.innerHTML = "<p class='font_error'>密码不能为空</p>";
      return false;
    } else if (new_password.length<6) {
      s_new1.setAttribute("style","display:block");
      s_new1.innerHTML = "<p class='font_error'>密码的长度至少6个字符</p>";
      return false;
    } else if (new_password.length>16) {
      s_new1.setAttribute("style","display:block");
      s_new1.innerHTML = "<p class='font_error'>密码的长度超过最大限制</p>";
      return false;
    }
    s_new1.setAttribute("style","display:none");
    return true;
  }


  function showPasswordError2()  {
    //4.提示信息
    var s_new2  = document.getElementById("s_new2");
    var confirm_password = document.getElementById("confirm_password").value
    var new_password = document.getElementById("new_password").value
    if (!confirm_password) {
      s_new2.setAttribute("style","display:block");
      s_new2.innerHTML = "<p class='font_error'>密码不能为空</p>";
      return false;
    } else if (confirm_password.length<6) {
      s_new2.setAttribute("style","display:block");
      s_new2.innerHTML = "<p class='font_error'>密码的长度至少6个字符</p>";
      return false;
    } else if (confirm_password.length>16) {
      s_new2.setAttribute("style","display:block");
      s_new2.innerHTML = "<p class='font_error'>密码的长度超过最大限制</p>";
      return false;
    } else if (confirm_password != new_password) {
      s_new2.setAttribute("style","display:block");
      s_new2.innerHTML = "<p class='font_error'>两次密码不一致</p>";
      return false;
    }
    s_new2.setAttribute("style","display:none");
    return true;
  }

</script>
</html>